import {put, select, takeEvery} from "redux-saga/effects";
import {types} from '@/store/home/Article';
import {errorMsg} from "@/util/global";
import {setArticleObjectValue} from "@/store/home/Article/action";
import {getPageList} from "@/api/artArticleInfo";

const effects = {
    * QueryArticleList() {
        try {
            let page = yield select(state => state.toJS().HomeArticle.ArticleCurrentPage);
            let size = yield select(state => state.toJS().HomeArticle.ArticleListPageSize);
            if (page === undefined || size === undefined ||
                parseInt(page) <= 0 || parseInt(size) <= '0') {
                return;
            }
            let obj = {
                page,
                size
            }
            const ajax = yield getPageList(obj);
            if (ajax.success === false) {
                return;
            }
            const data = {
                ArticleList: ajax.data.rows,
                ArticleCurrentPage: ajax.data.page,
                ArticleTotalPage: Math.ceil(ajax.data.total / size),
                ArticleListPageSize: ajax.data.size,
                ArticleCounts: ajax.data.total
            };
            yield put(setArticleObjectValue(data));
        } catch (e) {
            errorMsg("遇到未知错误，请重试！");
        }
    }
}

export default function* HomeArticle() {
    yield takeEvery(types.ARTICLE_QUERY, effects.QueryArticleList);
}
